<?php

	require_once("lib/settings.php");
	if(isset($_GET["modules"])) $module_code = urldecode($_GET["modules"]);
	else $module_code = false;
	if(isset($_GET["classes"])) $class_code = urldecode($_GET["classes"]);
	else $class_code = false;
	if(isset($_GET["students"]))$student_code = urldecode($_GET["students"]);
	else $student_code = false;
	
	if(isset($_GET["action"])) 
	{
		$action = $_GET["action"];
		switch($action)
		{	
			case "email":
				 include('lib/class.email.php');
				 $test = new email();
				 $test->emailClass($class_code, $module_code);
			break;
			case "criteria":
				$dbTable = new db_criterias();
				$dbTable->listCriterias($module_code, $student_code);
				$criteria = $dbTable->dump( false);
				$output = json_encode($criteria);
				$checksum = md5($output);
				$jsoncode =  json_encode(array("checksum" => $checksum, "data" => $criteria));
				$jsoncode = str_replace("criteria_name", "label", $jsoncode);
				$jsoncode = str_replace("criteria_id", "id", $jsoncode);
				echo $jsoncode;
			break;
			case "competenties":
				$dbTable = new db_competenties();
				$dbTable->listCompetenties($module_code, $student_code);
				$competenties = $dbTable->dump( false);
				$output = json_encode($competenties);
				$checksum = md5($output);
				$jsoncode =  json_encode(array("checksum" => $checksum, "data" => $competenties));
				$jsoncode = str_replace("competentie_naam", "label", $jsoncode);
				$jsoncode = str_replace("id", "id", $jsoncode);
				echo $jsoncode;
			break;
			case "students":
				$dbTable = new db_students();
				if($student_code)
				{
					$dbTable->search_student($student_code);
				}
				elseif($module_code)
				{
					$dbTable->listStudentRatings($module_code, $class_code);
				}
				else	
				{
					$dbTable->listStudents($module_code, $class_code);
				}
				$students = $dbTable->dump( false);

				$output = json_encode($students);
				$checksum = md5($output);
				$jsoncode =  json_encode(array("checksum" => $checksum, "data" => $students));
				$jsoncode = str_replace("student_name", "label", $jsoncode);
				$jsoncode = str_replace("mark", "highlight", $jsoncode);
				$jsoncode = str_replace("null", -1, $jsoncode);			//added by ruben, 21-02 -> werd anders lastig met js
				$jsoncode = str_replace("student_code", "id", $jsoncode);
				echo $jsoncode;
			break;
			case "modules":
				$dbTable = new db_modules();
				if($module_code)
				{
					$dbTable->search_module($module_code);
				}
				else	
				{
					$dbTable->listModules($student_code, $class_code);
				}
				$modules = $dbTable->dump( false);
				$output = json_encode($modules);
				$checksum = md5($output);
				$jsoncode =  json_encode(array("checksum" => $checksum, "data" => $modules));
				$jsoncode = str_replace("module_name", "label", $jsoncode);
				$jsoncode = str_replace("module_code", "id", $jsoncode);
				echo $jsoncode;
			break;
			case "classes":
				$dbTable = new db_classes();
				if($class_code)
				{
					$dbTable->search_classes($class_code);
				}
				else	
				{
					$dbTable->getClasses($student_code, $module_code);
				}
				$classes = $dbTable->dump( false);
				$output = json_encode($classes);
				$checksum = md5($output);
				$jsoncode =  json_encode(Array("checksum" => $checksum, "data" => $classes));
				$jsoncode = str_replace("class_code", "id", $jsoncode);
				echo $jsoncode;		
			break;
			case "addstudent":
				$student_name = $_POST['student_name'];
				$student_code = $_POST['student_code'];
				$classes = $_POST['classes'];
				if(!$student_name or !$student_code)
				{
					$alert;
					if(!student_name) $alert["student_name"] = 1;
					if(!student_code) $alert["student_code"] = 0;
					$output = json_encode($alert);
					echo $output;
				}
				else
				{				
					$alert;
					if($student_name && $student_code)
					{
						$dbTable = new db_students();
						$dbTable[0] = Array("student_code"=>$student_code, "student_name"=>$student_name);
						$result = $dbTable->save();
						if(is_int($result)) $alert["students"] = 1;
						if(!is_int($result)) $alert["students"] = 0;
					}
					if($classes)
					{
						$i = 0;
						$dbTable2 = new db_connect_student_class();
						foreach($classes as $class)
						{
							$dbTable2[$i] = Array("class_code"=>$class, "student_code"=>$student_code);
							$i++;
						}
						$result = $dbTable2->save();
						if(is_int($result)) $alert["classes"] = 1;
						if(!is_int($result)) $alert["classes"] = 0;
					}
					echo json_encode($alert);
				}
				
			break;
			case "addclass":
				$class_code = $_POST['class_code'];
				$students = $_POST['students'];
				$modules = $_POST['modules'];
				if(!$class_code)
				{
					$alert = Array("class_code"=>0);
					echo json_encode($alert);
				}
				else
				{
					$alert;
					if($class_code)
					{
						$dbTable = new db_classes();
						$dbTable[0] = Array("class_code"=>$class_code);
						$result = $dbTable->save();
						if(is_int($result)) $alert["classes"] = 1;
						if(!is_int($result)) $alert["classes"] = 0;					
					}
					if($students)
					{
						$i = 0;
						$dbTable2 = new db_connect_student_class();
						foreach($students as $student)
						{
							$dbTable2[$i] = Array("class_code"=>$class_code, "student_code"=>$student);
							$i++;
						}
						$result = $dbTable2->save();
						if(is_int($result)) $alert["students"] = 1;
						if(!is_int($result)) $alert["students"] = 0;
					}
					if($modules)
					{
						$i = 0;
						$dbTable3 = new db_connect_module_class();
						foreach($modules as $module) 
						{
							$dbTable3[$i] = Array("class_code"=>$class_code, "module_code"=>$module);
							$i++;
						}
						$result = $dbTable3->save();
						if(is_int($result)) $alert["modules"] = 1;
						if(!is_int($result)) $alert["modules"] = 0;
					}
					echo json_encode($alert);
				}				
			break;
			case "addrating":
				$student_code = $_POST['student_id'];
				$module_code = $_POST['module_id'];
				$mark = $_POST['mark'];
				$competenties = $_POST['competentions']; // array met rating_id, competentie_id, got_level
				$parts_of_rating = $_POST['part_of_rating']; // array met id's, commentaar, deelcijfer
				$alert;
				if($student_code && $module_code && $mark && $competenties && $parts_of_rating)
				{
					$dbTable = new db_ratings();
					$dbTable[0] = Array("student_code"=>$student_code, "module_code"=>$module_code, "mark"=>$mark);
					$rating_id = $dbTable->save();
					if(is_int($rating_id)) $alert["ratings"] = 1;
					if(!is_int($rating_id)) $alert["ratings"] = 0;
					
					$i = 0;
					$dbTable2 = new db_connect_rating_competentie();
					foreach($competenties as $competentie)
					{
						$dbTable2[$i] = Array("rating_id"=>$rating_id, "competentie_id"=>$competentie['competentie_id'], "got_level"=>$competentie['got_level']);
						$i++; 
					}	
					$result = $dbTable2->save();
					if(is_int($result)) $alert["rating_competentie"] = 1;
					if(!is_int($result)) $alert["rating_competentie"] = 0;
					
					$i = 0;
					$dbTable3 = new db_connect_criteria_rating();
					foreach($parts_of_rating as $part_of_rating)
					{
						$dbTable3[$i] = Array("criteria_id"=>$part_of_rating['id'], "rating_id"=>$rating_id, "comment"=>$part_of_rating['comment'], "mark"=>$part_of_rating['mark']);
						$i++; 
					}
					$result = $dbTable3->save();									
					if(is_int($result)) $alert["criteria_rating"] = 1;
					if(!is_int($result)) $alert["criteria_rating"] = 0;
					
					echo json_encode($alert);
				}
				else
				{
					$alert = array();
					if(!$student_code) $alert["student_code"] = 0;
					if(!$module_code) $alert["module_code"] = 0;
					if(!$mark) $alert["mark"] = 0;
					if(!$competenties) $alert["competenties"] = 0;
					if(!$parts_of_rating) $alert["parts_of_rating"] = 0;
					
					echo json_encode($alert);
				}							
			break;
			case "addmodule":
				$module_name = $_POST['module_name'];
				$module_code = $_POST['module_code'];
				$classes = $_POST['classes'];
				$criteria = $_POST['criteria'];
				$competentions = $_POST['competenties'];
				if(!$module_code or !$module_name)
				{
					$alert;
					if(!$module_code) $alert = Array("module_code"=>0);
					if(!$module_name) $alert = Array("module_name"=>0);
					echo json_encode($alert);
				}
				else
				{
					$alert;
					if($module_name && $module_code)
					{
						$dbTable = new db_modules();
						$dbTable[0] = Array("module_code"=>$module_code, "module_name"=>$module_name);
						$result = $dbTable->save();
						if(is_int($result)) $alert["modules"] = 1;
						if(!is_int($result)) $alert["modules"] = 0;
					}
					if($classes)
					{
						$i = 0;
						$dbTable2 = new db_connect_module_class();
						foreach($classes as $class)
						{
							$dbTable2[$i] = Array("class_code"=>$class, "module_code"=>$module_code);
							$i++;
						}
						$result = $dbTable2->save();
							if(is_int($result)) $alert["module_class"] = 1;
						if(!is_int($result)) $alert["module_class"] = 0;
					}
					if($criteria)
					{
						$i = 0;
						$dbTable3 = new db_connect_module_criteria();
						foreach($criteria as $crit)
						{
							$dbTable3[$i] = Array("criteria_id"=>$crit, "module_code"=>$module_code);
							$i++;
						}
						$result = $dbTable3->save();
							if(is_int($result)) $alert["module_criteria"] = 1;
						if(!is_int($result)) $alert["module_criteria"] = 0;
					}
					if($competentions)
					{
						$i = 0;
						$dbTable4 = new db_connect_module_competentie();
						foreach($competentions as $compentention)
						{
							$dbTable4[$i] = Array("competentie_id"=>$compentention['id'], "module_id"=>$module_code);
							$i++;
						}
						$result = $dbTable4->save();
						if(is_int($result)) $alert["competentions"] = 1;
						if(!is_int($result)) $alert["competentions"] = 0;
					}
					echo json_encode($alert);
				}
			break;
		}
	}

?>
